﻿using System;
using System.Web;
using System.Configuration;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;


namespace syc
{
    public class RuleModule : IHttpModule, IRequiresSessionState
    {
        /// <summary>
        /// </summary>

        #region IHttpModule Members

        public void Dispose()
        {
            //此处放置清除代码。
        }


        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
#if DEBUG
            return;
#endif
            HttpApplication App = (HttpApplication)sender;
            HttpContext context = App.Context;
            string script = context.Request.ServerVariables["SCRIPT_NAME"].ToLower();
            if (!script.EndsWith(".aspx")) return;

            string path = script.Substring(0, script.LastIndexOf('/') + 1);
            bool needlogin = false;
            string uid = (string)context.Session["OperatorID"];
            string power = (string)context.Session["OperatorPower"];
            string operatorname = (string)context.Session["OperatorName"];
            if (uid == null) uid = "";
            if (uid == "admin")
            {
                return;
            }
            if (power == null) power = "";
            if (operatorname == null) operatorname = "";

            if (path == "/")
            {
                char[] powers = power.ToCharArray();
                if (script == "/desktop.aspx" && operatorname == "")
                {

                    context.Response.Write(
@"<script type=text/javascript>       
parent.resetsubmenu('我的首页');
parent.appendsubmenu('我的桌面');
parent.addmenuitem('登录系统', '/Desktop.aspx', 0, '002');
parent.openmenu(parent.getfirstsubmenu());
</script>"
                    );
                    needlogin = true;
                }

                else if (script == "/schome.aspx")
                {
                    if (powers.Length < 1 || powers[0] == '0')
                        needlogin = true;
                }
                else if (script == "/fchome.aspx")
                {
                    if (powers.Length < 2 || powers[1] == '0')
                        needlogin = true;
                }
                else if (script == "/salehome.aspx")
                {
                    if (powers.Length < 3 || powers[2] == '0')
                        needlogin = true;
                }
                else if (script == "/xshome.aspx")
                {
                    if (powers.Length < 4 || powers[3] == '0')
                        needlogin = true;
                }
                else if (script == "/storehome.aspx")
                {
                    if (powers.Length < 5 || powers[4] == '0')
                        needlogin = true;
                }
                else if (script == "/sbjdhome.aspx")
                {
                    if (powers.Length < 6 || powers[5] == '0')
                        needlogin = true;
                }
                else if (script == "/jszlhome.aspx")
                {
                    if (powers.Length < 7 || powers[6] == '0')
                        needlogin = true;
                }
                else if (script == "/jcfxhome.aspx")
                {
                    if (powers.Length < 8 || powers[7] == '0')
                        needlogin = true;
                }
                else if (script == "/cwhshome.aspx")
                {
                    if (powers.Length < 9 || powers[8] == '0')
                        needlogin = true;
                }
                else if (script == "/settingshome.aspx")
                {
                    if (powers.Length < 10 || powers[9] == '0')
                        needlogin = true;
                }
                else if (script == "/adminhome.aspx")
                {
                    if (powers.Length < 11 || powers[10] == '0')
                        needlogin = true;
                }
            }
            else
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                Conn.Open();
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                Cmd.CommandText = "select [ID] from [系统功能] where [地址]='" + script.Replace("'","''")+ "'";
                Cmd.CommandType = CommandType.Text;
                object fid = Cmd.ExecuteScalar();
                Cmd.Dispose();
                Cmd.Dispose();
                Conn.Close();
                Conn.Dispose();

                if (fid != null)
                {
                    string of = (string)context.Session["OperatorFunction"];
                    if (of == null)
                    {
                        needlogin = true;
                    }
                    else
                    {
                        if (of.IndexOf(fid.ToString()) == -1)
                        {
                            needlogin = true;
                        }
                    }
                }
                else
                {
                    needlogin = true;
                }
            }
            if (needlogin)
            {
                context.Response.Write("<script>parent.dialog_login_reset('" + script + "');</script>");
                context.Response.End();
            }
        }


        #endregion


    }
}
